package main

import (
	"github.com/e421083458/golang_common/lib"
	"github.com/gogf/gf/os/glog"
	"github.com/rcrowley/go-metrics"
	"github.com/smallnest/rpcx/server"
	"github.com/smallnest/rpcx/serverplugin"
	"micro-client/conf"
	"micro-client/modules/mysql"
	"micro-client/vender/article/controller"
	"runtime"
	"time"
)

func main() {
	configPath := "./conf/pro/"
	if runtime.GOOS == "windows" {
		configPath = "./conf/dev/"
	}
	_ = lib.InitModule(configPath, []string{"base", "mysql"})
	defer lib.Destroy()

	// 配置信息 及 常量 加载到内存
	conf.PutIntoMemory()

	// 初始化 mysql
	_ = mysqlServer.InitDB()

	// 注册服务
	s := server.NewServer()
	for i := 0; i < len(conf.HttpConf.Server); i ++ {
		r := &serverplugin.ConsulRegisterPlugin{
			ServiceAddress: "tcp@" + conf.HttpConf.Addr + ":" + conf.HttpConf.Port,
			ConsulServers:  []string{conf.HttpConf.Server[i]},
			BasePath:       conf.HttpConf.Path[i],
			Metrics:        metrics.NewRegistry(),
			UpdateInterval: time.Minute,
		}
		err := r.Start()
		if err != nil {
			glog.Fatal(err)
		}
		s.Plugins.Add(r)
		_ = s.RegisterName("ModelConf", new(controller.ModelConf), "")
		_ = s.Serve("tcp", conf.HttpConf.Addr+":"+conf.HttpConf.Port)
	}
}
